package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMUtils2;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Slf4j
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping("sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpServletRequest request){
        String phone = user.getPhone();
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        //第三方发送信息校验
        log.info("code="+code);
        //SMUtils2.send(phone,code);
       // request.getSession().setAttribute("code",code);
         redisTemplate.opsForValue().set("code_"+phone,code,30, TimeUnit.SECONDS);
        return  R.success("验证码发送成功!");
    }

    @PostMapping("login")
    public R<User> login(@RequestBody Map<String,String> map,HttpServletRequest request){
      //从前端获取手机号
        String phone = map.get("phone");
        //获取验证码
        String code = map.get("code");
        //1.取出保存在session中的code
        //String code1 = (String) request.getSession().getAttribute("code");
        //2. 从redis数据库中取出系统生成的验证码
        String code1 = (String) redisTemplate.opsForValue().get("code_" + phone);
        //和前端输入的code比较
        boolean flag = StringUtils.equals(code, code1);
        //不相同  return R.error
        if(!flag){
            return R.error("验证码错误!");
        }
        //是否是新用户,不是new user 存入数据库
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery(User.class)
                .eq(User::getPhone, phone);
        User user = userService.getOne(queryWrapper);
        if(user==null){
            user = new User();
            user.setPhone(phone);
            userService.save(user);
        }

        //将用户id存入session中
        request.getSession().setAttribute("userId",user.getId());
        //删除redis里面的已经被验证过的验证码
        redisTemplate.delete("code_"+phone);
        return R.success(user);
    }
    @PostMapping("loginout")
    public R<String> loginout(HttpServletRequest request){
      request.getSession().invalidate();
      return R.success("退出成功");
    }
}
